package com.yiban.demo.Access.Controller;

import com.yiban.demo.Global.AOP.Annotation.CheckList;
import com.yiban.demo.Access.Entity.Access;
import com.yiban.demo.Global.Entity.EntityGroup.NeedIdGroup;
import com.yiban.demo.Global.Entity.EntityGroup.NoNeedIdAccessGroup;
import com.yiban.demo.Global.Entity.ResponseEntity;
import com.yiban.demo.Access.Entity.RestfulUri;
import com.yiban.demo.Access.Service.AccessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 权限控制
 * @author 10
 */

@RestController
@RequestMapping("/Access")
public class AccessController {
    @Autowired
    private AccessService accessService;

    /**
     * 获取所有权限
     * @return 包含权限对uri的一一映射
     *
     */
    @GetMapping("/allAccess")
    public ResponseEntity<Map<String, HashSet<RestfulUri>>> getNowUrlManagement(){
        final Map<String, HashSet<RestfulUri>> map = accessService.getAllAccess();
        return ResponseEntity.Success(map);
    }

    /**
     * 刷新当前最新的权限控制
     * @return 成功
     */
    @GetMapping("/refresh")
    public ResponseEntity<Boolean> refresh(){
        accessService.refresh();
        return ResponseEntity.Success(true);
    }

    /**
     * 更新一个权限的信息
     * @param access 需要更新的，必须填写access_id属性
     * @return 更新是否成功
     */
    @PutMapping(value = "/access",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Boolean> update(@Validated(NeedIdGroup.class) @RequestBody Access access){

        List<String> errors = access.checkField(false);
        if (!errors.isEmpty()){
            return ResponseEntity.Failure(errors.stream().collect(Collectors.joining("/n")));
        }
        return accessService.updateAccessById(access) ? ResponseEntity.Success(true):ResponseEntity.Failure("此id不存在");
    }

    /**
     * 根据id删除一个权限信息
     *
     * @version 1.1
     * @otherDesc url格式为/accesses/1,2,3,4,5
     * @param list 批量删除的id集合
     * @return 受到影响的条数
     */

    @DeleteMapping("/accesses/{access_ids}")
    public ResponseEntity<Integer> delete(@PathVariable("access_ids") List<Integer> list){
        return ResponseEntity.Success(accessService.deleteAccesses(list));
    }

    /**
     * 添加若干个权限
     * @param list 需要添加的权限集合
     * @return 影响的条数
     */
    @CheckList(group = NoNeedIdAccessGroup.class)
    @PostMapping(value = "/accesses",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Integer> adds(@RequestBody List<Access> list){
        String s = "第%d个存在 %s 问题";
        StringJoiner error = new StringJoiner("\n");
        for (int i = 0; i < list.size(); i++) {
            final List<String> info = list.get(i).checkField(true);
            if (!info.isEmpty()){
                error.add(String.format(s, i + 1, info.stream().collect(Collectors.joining("\n"))));
            }
        }
        if (error.length() != 0){
            return ResponseEntity.Failure(error.toString());
        }
        return ResponseEntity.Success(accessService.addAccesses(list));
    }

    /**
     * 获取当前所有权限信息
     * @return 当前存在的权限集合
     */
    @GetMapping("/roles")
    public ResponseEntity<List<String>> getAllRoles(){
        return ResponseEntity.Success(accessService.getAllRole());
    }




}
